(function($){
	$.Workplace = function(){                                             //定义函数
		var screenX, screenH, documentH;
		var t = '';
		var opt = {
			callback: function(val, text){}
		};
		var option;
		this.showextend = function(randfrm, arg){
			option = $.extend(opt, arg);		//合并数组对象
			this.showlocInput(randfrm);
		}
		var othis = this;
		var bool_town = true;
		var isIE6 = !!window.ActiveXObject && !window.XMLHttpRequest;

		//---------------------工作地区---------------------------------------------------------------------------------------------------
		//加载内容
		this.showlocInput = function(randfrm){
			if(($(option.objId).html() == undefined || $(option.objId).html() == 'null')){
				this.keeploc_con(randfrm);			//遮掩弹出
			}else{
				this.nextshow(randfrm);
			}
			this.keep_sceen();							//显示遮掩层
			this.layer();								//显示弹出层函数
		};

		//显示加载内容窗口
		this.keeploc_con = function(randfrm){
			var str = '';
			str += '<div class="layer posCategories" id="jobLoc' + randfrm + '">';
			str += '<div class="winTitle poswin"> <span class="one">工作地区</span> <span class="two"><span class="close">×</span></span> </div>';
			str += '<div class="first">';
			str += '<div class="one"><div class="morepos">您还没有选择工作地区，未选择工作地区不能发布职位。</div>';
			str += '<span class="btnok" style="margin-left:20px;"><a href="javascript:void(0);">确定</a></span></div>';
			str += '<div class="two"><div class="cselect">已选职位：</div>';
			str += '<div class="poscate" id="locCate' + randfrm + '"></div><div class="btnok"><a href="javascript:void(0);" >确定</a></div></div>';
			str += '</div>';

			str += '<div class="workplce" id="workplce">';
			str += '<div class="wleft"><span>选择地区</span></div>';
			str += '<div class="wright" id="wsheng">';

			str += '<select name="address_P' + randfrm + '"  id="address_P' + randfrm + '">';
			str += '<option value="">请选择</option>';
			str += '</select>';

			str += '<select name="address_C' + randfrm + '"  id="address_C' + randfrm + '">';
			str += '<option value="">请选择</option>';
			str += '</select>';

			str += '</div>';
			str += '</div>';

			//循环类型
			str += '<div class="cateType">';
			str += '</div>';
			str += '</div>';
			$("body").append(str);	//追加到页面

			//ajax加载省市
			$.ajax({
				type: "GET",
				url: "/common/province/",
				dataType: "json",
				success: function(data){
					jqueryAddOption(option.objProvince, data, "jobLoc" + randfrm);

					//判断工作地区是不是存在
					var newArr = [];
					for(i = 0; i < option.objArr.length; i++){
						if($(option.objArr[i]).val() != '' && $(option.objArr[i]).val() != '0'){
							newArr.push($(option.objArr[i]).val());
						}
					}
					//获取2级数组
					var areaArr = [];
					for(i = 0; i < newArr.length; i++){
						areaArr.push(newArr[i].substring(0, 4) + '00');
					}
					areaArr = array_merge(areaArr);
					//判断是不是有表单传过来的值
					if(areaArr.length > 0){
						for(var i = 0; i < areaArr.length; i++){
							var areaid = areaArr[i];
							var pid = areaArr[i].substring(0, 2) + '0000';
							othis.changeProvince(pid, randfrm, areaid);
						}
					}else{
						var curr_p = 'auto';					//330000代表当前的省份是浙江
						$(option.objId).find('#' + option.objProvince).val(curr_p);
						var curr_c = 'auto';
						othis.changeProvince(curr_p, randfrm, curr_c);
					}
				},
				error: function(){
					layer.msg('网络原因引起的错误，请重新加载', {time: 1500});
				}
			});

			//一级省份绑定事件
			$(option.objId).find('#' + option.objProvince).change(function(){
				var addre_P = this.options[this.options.selectedIndex].value;
				othis.changeProvince(addre_P, randfrm, '');
			});
			//二级市区绑定事件
			$(option.objId).find('#' + option.objCity).change(function(){
				var addre_C = this.options[this.options.selectedIndex].value;
				var addre_Text = this.options[this.options.selectedIndex].text;
				othis.changeTwoProvince(addre_C, addre_Text, randfrm, 2);
			});
			//点击确定按钮
			$(option.objId).find('a').click(function(){
				inpdetermines();	//外面放在input表单里面点击确定按钮的函数
			})
		}

		//选择省获取市区县级市
		this.changeProvince = function(addre_P, randfrm, curr_c){
			//清空select
			$(option.objId).find('#' + option.objCity).empty();
			$(option.objId).find('#' + option.objCity).append($("<option/>", {value: '', text: '请选择'}));
			//选择为空时
			if(!addre_P) return false;
			var selval = '';
			//定义数组
			var otherprovince = [];
			otherprovince[110000] = "北京";
			otherprovince[310000] = "上海";
			otherprovince[120000] = "天津";
			otherprovince[500000] = "重庆";
			otherprovince[810000] = "香港";
			otherprovince[820000] = "澳门";
			otherprovince[710000] = "台湾";

			for(var i in otherprovince){
				if(i == addre_P){
					selval = 1;
					$(option.objId).find('#' + option.objCity).empty();
					$(option.objId).find('#' + option.objCity).append($("<option/>", {value: '', text: '请选择'}));
					$(option.objId).find('#' + option.objCity).append($("<option/>", {
						value: i,
						text: otherprovince[i]
					}));

					if(curr_c != ''){
						$(option.objId).find('#' + option.objProvince).val(addre_P);
						$(option.objId).find('#' + option.objCity).val(addre_P);
						var addre_Text = $(option.objId).find('#' + option.objCity).find("option:selected").text();
						othis.changeTwoProvince(addre_P, addre_Text, randfrm, 1); //追加当前市区到内容
					}
				}
			}
			if(selval != '1'){
				$.ajax({
					type: "GET",
					url: "/common/two_area/" + addre_P,
					dataType: "json",
					success: function(data){
						if(data.areas.length > 0){
							$(option.objId).find('#' + option.objCity).empty();
							$(option.objId).find('#' + option.objCity).append($("<option/>", {value: '', text: '请选择'}));
							if(addre_P == 'auto'){
								jqueryAddOption(option.objCity, data.areas, "jobLoc" + randfrm);
								$(option.objId).find('#' + option.objProvince).val(data.provincecid);
								$(option.objId).find('#' + option.objCity).val(data.areaid);
								var addre_Text = $(option.objId).find('#' + option.objCity).find("option:selected").text();
								othis.changeTwoProvince(data.areaid, addre_Text, randfrm, 1); //追加当前市区到内容
							}else{
								jqueryAddOption(option.objCity, data.areas, "jobLoc" + randfrm);
								//模拟选中
								if(curr_c != ''){
									$(option.objId).find('#' + option.objProvince).val(addre_P);
									$(option.objId).find('#' + option.objCity).val(curr_c);
									var addre_Text = $(option.objId).find('#' + option.objCity).find("option:selected").text();
									othis.changeTwoProvince(curr_c, addre_Text, randfrm, 1); //追加当前市区到内容
								}
							}
						}
					},
					error: function(){
						layer.msg('网络原因引起的错误，请重新加载', {time: 1500});
					}
				});
			}
		}

		//-----------------------------------------------------------------------------------------------------------------------------
		//追加当前市区县级市内容
		this.changeTwoProvince = function(addre_C, addre_Text, randfrm, otype){
			var dataid = [];
			var datanum = '';
			$(option.objId + ' .cateType dt').each(function(){
				dataid.push($(this).find('span').attr('data-id'));
			});
			//判断是不是存在
			if(dataid.length > 0){
				$.each(dataid, function(key, value){
					if(value == addre_C){
						datanum = 1;
						return false;
					}
				});
			}

			//判断是不是已经存在
			if(datanum != '1'){
				//判断是不是只有二级菜单
				var otherprovince = [110000, 310000, 120000, 500000, 810000, 820000, 710000];
				var city = in_array(addre_C, otherprovince) ? 'two_area' : 'two_city';
				$.ajax({
						type: 'GET',
						url: '/common/' + city + '/' + addre_C,
						dataType: 'json',
						success: function(data){
							if(data.areas.length>0){
								var str = '';
								//当前县级市、
								str += '<dl id="p' + addre_C + '">';
								str += '<dt><span data-id="' + addre_C + '">' + addre_Text + '</span></dt>';
								str += '<dd><ul>';
								//添加市区不限
								var areaText = addre_Text + '-不限';
								str += '<li><div class="oneSelect">';
								str += '<input name="jobLocations[]" id="jobLocations' + randfrm + addre_C + '" class="checkboxs" data-name="' + areaText + '" type="checkbox" value="' + addre_C + '" />';
								str += '<label for="jobLocations' + randfrm + addre_C + '">' + areaText + '</label>';
								str += '</div></li>';
								$.each(data.areas, function(key, value){
									str += '<li><div class="oneSelect">';
									str += '<input name="jobLocations[]" id="jobLocations' + randfrm + value.value + '" class="checkboxs" data-name="' + value.text + '" type="checkbox" value="' + value.value + '" />';
									str += '<label for="jobLocations' + randfrm + value.value + '">' + value.text + '</label>';
									str += '</div></li>';
								});
								str += '</ul></dd>';
								str += '</dl>';
								$(option.objId + " .cateType").prepend(str);
								//点击Input表单事件
								$(option.objId).find('.cateType').find('#p' + addre_C).find("input[type='checkbox']").click(function(){
									clickInput($(this));
								});
								//当前乡镇街道
								if(data.status == 1 && data.towns.town.length > 0 && bool_town){
									var town = '';
									town += '<dl id="p' + data.towns.area.value + '">';
									town += '<dt><span data-id="' + data.towns.area.value + '">' + data.towns.area.text + '</span></dt>';
									town += '<dd><ul>';
									//添加县级市不限
									var townText = data.towns.area.text + '-不限';
									town += '<li><div class="oneSelect">';
									town += '<input name="jobLocations[]" id="jobLocations' + randfrm + data.towns.area.value + '" class="checkboxs" data-name="' + townText + '" type="checkbox" value="' + data.towns.area.value + '" />';
									town += '<label for="jobLocations' + randfrm + data.towns.area.value + '">' + townText + '</label>';
									town += '</div></li>';
									$.each(data.towns.town, function(key, value){
										town += '<li><div class="oneSelect">';
										town += '<input name="jobLocations[]" id="jobLocations' + randfrm + value.value + '" class="checkboxs" data-name="' + value.text + '" type="checkbox" value="' + value.value + '" />';
										town += '<label for="jobLocations' + randfrm + value.value + '">' + value.text + '</label>';
										town += '</div></li>';
									});
									town += '</ul></dd>';
									town += '</dl>';
									//标记存在时不设置
									bool_town = false;
									$(option.objId + " .cateType").prepend(town);
									//点击Input表单事件
									$(option.objId).find('.cateType').find('#p' + data.towns.area.value).find("input[type='checkbox']").click(function(){
										clickInput($(this));
									});
								}

								//设置弹出层的高度
								othis.setHeight();

								//选择的类型
								if(otype == '1'){
									othis.nextshow(randfrm);
								}
								othis.keep_sceen();							//显示遮掩层
								othis.layer();								//显示弹出层函数
							}
						},
						error: function(){
							layer.msg('可能网络原因出现错误');
						}
					}
				);
			}else{
				layer.msg('您已经添加了这个,请重新选择地区', {time: 1500});
			}
		}

		//设置高度
		this.setHeight = function(){
			//设置弹出层的高度
			$(option.objId).find('.cateType').find('dd').each(function(index){
				var key = $(this).find('li').length;
				var indexkey = Math.ceil(key / 4);
				var liHeight = $(this).find('li').height();
				$(this).css({"height": (liHeight * indexkey) + 'px'});
				$(this).parent('dl').find('dt').css({
					"height": (liHeight * indexkey) + 'px',
					"background-color": '#f5f5f5'
				});
			});
		}

		//-----------------------------------------------------------------------------------------------------------------------------
		//显示遮掩层函数
		this.layer = function(){
			screenX = $(window).width();
			screenH = $(window).height();
			documentH = $(document).height();
			iLeft = parseInt((screenX - $(option.objId).width()) / 2);
			iTop = parseInt((screenH - $(option.objId).height()) / 2);
			if(screenX < $(option.objId).width()){
				screenX = $(option.objId).width();
				iLeft = 0;
			}
			$(option.objId).css({'left': iLeft, 'top': iTop}).show();		//对象显示
			$(option.objId).find('.two .close').click(function(){		//点击关闭按钮时，关闭窗口
				$(option.keep_sceen).hide();
				$(option.objId).hide();
			});
			//div遮掩层
			$(option.keep_sceen).css({
				"width": screenX + 'px',
				"height": documentH + 'px',
				'background': '#111111',
				'opacity': 0.5
			}).show();

			//判断IE6
			if(isIE6){
				$(option.keep_sceen).css({
					position: "absolute",
					"width": screenX + 'px',
					"height": documentH + 'px',
					left: 0,
					top: 0,
					'z-index': '1000'
				}).show();

				$(option.objId).css({
					position: "absolute",
					'left': iLeft,
					'top': iTop,
					'z-index': '1001'
				});   //对象显示

			}

			//窗口压缩事件触发
			$(window).resize(function(){
				screenX = $(window).width();
				screenH = $(window).height();
				documentH = $(document).height();
				iLeft = parseInt((screenX - $(option.objId).width()) / 2);
				iTop = parseInt((screenH - $(option.objId).height()) / 2);
				if(screenX < $(option.objId).width()){
					screenX = $(option.objId).width();
					iLeft = 0;
				}
				if(screenH < $(option.objId).height()){
					screenH = documentH;
					iTop = 0;
				}else{
					screenH = documentH;
				}
				$(option.keep_sceen).css({"width": screenX, "height": screenH});
				$(option.objId).css({"left": iLeft, "top": iTop});
			});
		}


		//显示弹出层函数
		this.nextshow = function(randfrm){
			var checkboxObj = $(option.objId + " input[type='checkbox']");
			checkboxObj.attr('checked', false);
			$(option.objId + ' .ctype').remove();
			$(option.objId + ' .cateType').find('li label').css({"color": "#0066CC"});
			var newArr = [];
			for(i = 0; i < option.objArr.length; i++){
				if($(option.objArr[i]).val() != '' && $(option.objArr[i]).val() != '0'){
					newArr.push($(option.objArr[i]).val());
				}
			}
			//去除相同的值
			newArr = array_merge(newArr);
			if(newArr.length > 0){
				for(i = 0; i < newArr.length; i++){
					(function(x){	//使用闭包函数  把i 作用在这个函数体内
						var thisval = newArr[x];
						//设置样式
						$(option.objId).find('dl').find("input[name='jobLocations[]'][value='" + thisval + "']").attr('checked', true);
						$(option.objId).find('dl').find("input[name='jobLocations[]'][value='" + thisval + "']").next().css({"color": "#800080"});
						pcate = $(option.objChcekbox + thisval).attr('data-name');
						poscate = '<div class="ctype" id="' + option.objSelect + thisval + '"> <span>' + pcate + '</span><i></i></div>';
						$(option.objCate).append(poscate);
						//点击i关闭事件
						$(option.objId).find(option.objCate).find("#" + option.objSelect + thisval).find('i').click(function(){
							objClose(thisval);
						});
					})(i);
				}
			}
			if(newArr.length > 0){
				$(option.objId + ' .first').find('.one').hide();
				$(option.objId + ' .first').find('.two').show();
			}else{
				$(option.objId + ' .first').find('.one').show();
				$(option.objId + ' .first').find('.two').hide();
			}
		}
		//去除数组中相同的元素
		this.undulpicate = function(array){
			for(var i = 0; i < array.length; i++){
				for(var j = i + 1; j < array.length; j++){
					//注意 ===
					if(array[i] === array[j]){
						array.splice(j, 1);
						j--;
					}
				}
			}
			return array;
		}

		//@parm othis 当前表单 私有函数点击input事件
		//input 表单点击事件
		function clickInput(othis){
			var checked_array = [];
			$(option.objId + " input[type='checkbox']:checked").each(function(){
				checked_array.push($(this).val());
			});
			//删除重复值
			checked_array = array_merge(checked_array);
			if(checked_array.length > option.objInpaLength){
				$(option.objChcekbox + othis.val()).attr('checked', false);
				layer.msg('最多只能选择' + option.objInpaLength + '个', {time: 1000});
				return false;
			}
			if(checked_array.length >= 0 && checked_array.length <= option.objInpaLength){
				$(option.objId + ' .first').find('.one').hide();
				$(option.objId + ' .first').find('.two').show();

				if(othis.is(':checked')){
					//点击input选中
					var thisval = othis.val();
					$(option.objId).find('dl').find("input[name='jobLocations[]'][value='" + thisval + "']").attr('checked', true);
					$(option.objId).find('dl').find("input[name='jobLocations[]'][value='" + thisval + "']").next().css({"color": "#800080"});

					pcate = $(option.objChcekbox + thisval).attr('data-name');
					poscate = '<div class="ctype" id="' + option.objSelect + thisval + '"><span>' + pcate + '</span><i></i></div>';
					$(option.objCate).append(poscate);

					//点击i关闭事件
					$(option.objId).find(option.objCate).find("#" + option.objSelect + thisval).find('i').click(function(){
						objClose(thisval);
					});

				}else{
					thisval = othis.val();
					$(option.objId).find('dl').find("input[name='jobLocations[]'][value='" + thisval + "']").attr('checked', false);
					$(option.objId).find('dl').find("input[name='jobLocations[]'][value='" + thisval + "']").next().css({"color": "#0066CC"});

					if(!$(option.objId).find('dd').find('li input').is(':checked')){
						$(option.objId + ' .first').find('.one').show();
						$(option.objId + ' .first').find('.two').hide();
					}
					poscate = $('#' + option.objSelect + othis.val());
					poscate.remove();
				}
			}else{
				$(option.objId + ' .first').find('.one').show();
				$(option.objId + ' .first').find('.two').hide();
				$(option.objId + ' .ctype').remove();
			}
		}

		//@parm okey 当前input键值 Val值
		//点击i 关闭事件
		function objClose(okey){
			$(option.objId).find('dl').find("input[name='jobLocations[]'][value='" + okey + "']").attr('checked', false);
			$(option.objId).find('dl').find("input[name='jobLocations[]'][value='" + okey + "']").next().css({"color": "#0066CC"});
			$('#' + option.objSelect + okey).remove();
			if($(option.objId + " input[type='checkbox']:checked").size() <= 0){
				$(option.objId + ' .first').find('.one').show();
				$(option.objId + ' .first').find('.two').hide();
			}
		}

		//**********外面放在input表单里面点击确定按钮的函数***********************************************************************************
		function inpdetermines(){
			var strjoin = '';
			$(option.objPostCate).val();
			var checkboxObj = $(option.objId + " input[type='checkbox']");
			var input_val;
			var jobArray = new Array();
			var joinArray = new Array();
			$.each(checkboxObj, function(key, value){
				if(checkboxObj[key].checked == true){
					input_val = checkboxObj.eq(key).val();
					jobArray.push(input_val);
				}
			});
			//去除相同的值
			jobArray = array_merge(jobArray);

			/**清空数组里面的值**/
			for(i = 0; i < option.objArr.length; i++){
				$(option.objArr[i]).val("");
			}
			if(jobArray.length > 0){
				for(i = 0; i < jobArray.length; i++){
					$(option.objArr[i]).val(jobArray[i]);
					pcate = $(option.objChcekbox + jobArray[i]).attr('data-name');
					joinArray.push(pcate);
				}
			}else{
				strjoin = '';
			}

			//以+号链接字符串
			strjoin = joinArray.join("+");
			$(option.objPostCate).val(strjoin);

			$(option.keep_sceen).hide();
			$(option.objId).hide();
			option.callback(jobArray, strjoin);
		}

		//判断对象是不是存在
		this.keep_sceen = function(){
			if($("#keep_sceen").length <= 0){
				$("body").append('<div class="cover" id="keep_sceen" style="z-index:1000;position:absolute"></div>');
			}
		}

	}
	;
})
(jQuery);
var workObj = new $.Workplace();
//--------------------------------创建工作地区对象----------------------------------------------------------------------------
function workplaceInput(randfrm){
	var workarr = {
		objId: '#jobLoc' + randfrm,					//当前显示内容的ID值
		objProvince: 'address_P' + randfrm,	        //当前select省份
		objCity: 'address_C' + randfrm,		        //当前select市区县
		objCate: '#locCate' + randfrm,					//当前页面弹出层显示要追加的工作地区类别内容
		objTwoCate: '#twolocCate' + randfrm,		    //当前页面要追加的工作地区类别内容
		objChcekbox: '#jobLocations' + randfrm,	    //当前弹出层 复选框checkbox的前缀名id值
		objSelect: 'locSelect' + randfrm,				//当前弹出层 追加到弹出要追加的内容里面的ID前缀值
		objTwoSelect: 'twolocSelect' + randfrm,	    //当前页面  追加到当前页面要追加的内容里面的ID前缀值
		objPostCate: '#workplace',					//当前页面  追加到当前页面input要追加的内容里面的ID前缀值
		objArr: ["#jobarea1" + randfrm, "#jobarea2" + randfrm, "#jobarea3" + randfrm, "#jobarea4" + randfrm, "#jobarea5" + randfrm], //当前隐藏表单
		keep_sceen: '#keep_sceen',					//遮掩层
		objLevel: '3',								//设置当前显示的级别
		objInpaLength: '5'							//设置当前input 表单选项的个数
	};
	workObj.showextend(randfrm, workarr);
}